package com.zrh.pma.pma.controllers;

import com.zrh.pma.pma.entities.Project;
import com.zrh.pma.pma.entities.Student;
import com.zrh.pma.pma.services.ProjectService;
import com.zrh.pma.pma.services.StudentSeivice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@Controller
@RequestMapping("projects")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    StudentSeivice studentSeivice;
    @GetMapping
    public String displayProject(Model model){
        List<Project> projects = projectService.getAll();
        model.addAttribute("projects",projects);
        return "projects/list-projects";
    }
    @GetMapping("/new")
    public  String displayProjectForm   (Model model)
    {
        Project aProject=new Project();
        List<Student> students = studentSeivice.getAll();
        model.addAttribute("project",aProject);
        model.addAttribute("allStudents",students);

        return  "projects/new-project";
    }

    @PostMapping("/save")
    public String createProject(Model model,@RequestParam List<Long> students,  Project project){

        projectService.save(project);



        return "redirect:/projects";
    }
}
